package com.vhuan.self_discipline_service.domain.battery.po;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;

import java.io.Serial;
import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.vhuan.common.constant.battery.BatteryPeriodType;
import com.vhuan.common.constant.common.Grade;
import com.vhuan.self_discipline_service.domain.AbstractFocusBaseEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;

/**
 * <p>
 * 电池表
 * </p>
 *
 * @author xch
 * @since 2024-11-05
 */
@EqualsAndHashCode(callSuper = true)
@Data
@TableName("battery_period")
public class BatteryPeriodEntity extends AbstractFocusBaseEntity {

    @Serial
    private static final long serialVersionUID = 1L;

    private Long batteryId;

    @JsonFormat(timezone = "${pattern.timezone}",pattern = "${pattern.dateformat}")
    @DateTimeFormat(pattern = "${pattern.dateformat}")
    private LocalDateTime startTime;

    @JsonFormat(timezone = "${pattern.timezone}",pattern = "${pattern.dateformat}")
    @DateTimeFormat(pattern = "${pattern.dateformat}")
    private LocalDateTime endTime;

    private Integer capacity;

    private Integer maxCapacity;

    private String resetPeriod;
    @TableField(value = "is_over")
    private Integer overFlag;

    private String grade;

    /**
     * 电池周期的构造函数，根据电池对象初始化开始时间和结束时间
     * @param battery 电池对象
     */
    public BatteryPeriodEntity(BatteryEntity battery) {
        this.setBatteryId(battery.getId());
        this.setCapacity(battery.getMaxCapacity());
        this.setResetPeriod(battery.getResetPeriod());
        this.setMaxCapacity(battery.getMaxCapacity());
        // 根据电池的重置周期类型设置开始时间和结束时间
        switch (battery.getResetPeriod()) {
            case BatteryPeriodType.YEAR:
                calculateYearResetTime(LocalDateTime.now());
                break;
            case BatteryPeriodType.MONTH:
                calculateMonthResetTime(LocalDateTime.now());
                break;
            case BatteryPeriodType.WEEK:
                calculateWeekResetTime(LocalDateTime.now());
                break;
            default:
                throw new IllegalArgumentException("Unsupported reset period type: " + battery.getResetPeriod());
        }
    }

    // 辅助方法：计算年重置周期的时间
    private void calculateYearResetTime(LocalDateTime createTime) {
        // 实现月重置周期的计算逻辑
        this.startTime =createTime.withDayOfYear(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
        this.endTime =createTime.with(TemporalAdjusters.lastDayOfYear()).withHour(23).withMinute(59).withSecond(59).withNano(999999999);
    }

    // 辅助方法：计算月重置周期的时间
    private void calculateMonthResetTime(LocalDateTime createTime) {
        // 实现月重置周期的计算逻辑
        this.startTime =createTime.withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
        this.endTime =createTime.with(TemporalAdjusters.lastDayOfMonth()).withMinute(59).withSecond(59).withNano(999999999);
    }

    // 辅助方法：计算周重置周期的时间
    private void calculateWeekResetTime(LocalDateTime createTime) {
        // 实现周重置周期的计算逻辑
        this.startTime = createTime.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY))
                .withHour(0)
                .withMinute(0)
                .withSecond(0)
                .withNano(0);

        this.endTime = createTime.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY))
                .withHour(23)
                .withMinute(59)
                .withSecond(59)
                .withNano(999999999);
    }

    /**
     *  生成下一周期的batteryPeriod
     * @param batteryPeriod 上一周期的batteryPeriod
     */
    public BatteryPeriodEntity(BatteryPeriodEntity batteryPeriod){
        this.setBatteryId(batteryPeriod.getBatteryId());
        this.setCapacity(batteryPeriod.getMaxCapacity());
        this.setResetPeriod(batteryPeriod.getResetPeriod());
        this.setMaxCapacity(batteryPeriod.getMaxCapacity());
        // 根据电池的重置周期类型设置开始时间和结束时间
        switch (batteryPeriod.getResetPeriod()) {
            case BatteryPeriodType.YEAR:
                calculateYearResetTime(LocalDateTime.now());
                break;
            case BatteryPeriodType.MONTH:
                calculateMonthResetTime(LocalDateTime.now());
                break;
            case BatteryPeriodType.WEEK:
                calculateWeekResetTime(LocalDateTime.now());
                break;
            default:
                throw new IllegalArgumentException("Unsupported reset period type: " + batteryPeriod.getResetPeriod());
        }
    }

    public void setGradeByCapacity() {
        double percentage = (double) capacity / maxCapacity;
        this.grade = percentage >= 0.75 ? Grade.perfect : percentage >= 0.5 ? Grade.good : percentage >= 0.25 ? Grade.normal : Grade.bad;
    }
}
